#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: actions.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Action manager for Vazaar

import os
import datetime
from gettext import gettext as _
import pynotify

#import gobject
import gtk

from rdflib import Literal

#import model
#from namespaces import NSBINDINGS
from vazaarlib.utils import get_logger, beautiful_name, get_date_filtered_query
from vazaarlib.utils import get_filtered_query, get_predicate_object_date_filtered_query
from vazaarlib.job import Job
#from vresource import VirtualResource
from vazaarlib.ui.widgets.about import AboutDialog
from vazaarlib.ui.widgets.cloud import Cloud
from vazaarlib.ui.helpers import *
from vazaarlib.constants import VAZAAR_USER_RESOURCES_DIR


class ActionManager():
    def __init__(self, gui):
        self.gui = gui
        self.log = get_logger("ActionMgr")

    def get_date_filter(self):
        model = self.gui.frontend['sh-cmbfdate'].get_model()
        iter = self.gui.frontend['sh-cmbfdate'].get_active_iter()

        try:
            filter = model.get_value(iter, 0)
            if filter == '-':
                # avoid date filtering
                return 0
            else:
                return filter
        except Exception, error:
            return 0

    def notify(self, header, text, icon=None):
        n = pynotify.Notification(header, text)
        n.set_icon_from_pixbuf(icon)
        n.show()

    def add_resource(self, data=None):
        jobId = self.gui.job_counter
        self.gui.job_counter += 1
        job = Job(jobId, data)
        self.update_statusbar("<b>Adding a new resource...</b>")
        self.gui.queue['in'].put(job)

    def delete_data_resource(self, id):
        try:
            # delete file from OS.
            filename = VAZAAR_USER_RESOURCES_DIR + '/' + id[9:]
            os.unlink(filename)
        except:
            pass # It's normal if it doesn't exist

    def unlink_from_collection(self, id, col):
        cols = self.gui.ask.collections_linked_to_resource(id)
        pass

    def cancel_import(self):
        self.gui.app.provider.set_running(False)
        self.gui.app.cancel_current_jobs()
        self.update_statusbar("All current jobs have been canceled")


    def __feed(self):
        for sub, pred, obj in self.gui.provider.store:
            self.gui.graph.add_triple(sub, pred, obj)
        self.gui.graph.commit()

    def check_incoming_jobs(self):
        print "check_incoming_jobs disabled"
        """
        if not self.gui.property['working']:
            #print "Items in the incoming queue: ", self.gui.queue['in'].qsize()
            if self.gui.queue['in'].qsize() >= 1:
                self.gui.property['working'] = True
                job = self.gui.queue['in'].get()
                self.gui.provider.prepare(job)
                #gtk.gdk.thread
                self.gui.provider.run(self.gui.property['counter'])
                self.__feed()
                self.gui.property['counter'] += 1
                self.gui.queue['out'].put(job)
        return True
        """

    def check_outcoming_jobs(self):
        #print self.gui.queue['out'].qsize()
        if self.gui.queue['out'].qsize() >= 1:
            job = self.gui.queue['out'].get()
            self.gui.property['working'] = False
            self.update_gui()
        else:
            self.gui.property['working'] = False

        return True


    def every_runs(self):
        #self.update_gui()
        self.unfullscreen()
        self.setup_date_filter()
        #self.gui.frontend['sh-btnclp'].set_active(True)
        #self.gui.frontend['sh-clipboard'].hide_all()
        #self.gui.frontend['sh-editres'].hide_all()



    def display_resource(self, id):
        self.gui.manager.display_resource(id)


    def first_run(self):
        pass
        #check_environment()

    def fullscreen(self):
        self.gui.frontend['menu'].hide_all()
        icon = gtk.image_new_from_stock(gtk.STOCK_LEAVE_FULLSCREEN, gtk.ICON_SIZE_BUTTON)
        self.gui.frontend['mainwin'].fullscreen()
        self.gui.frontend['fullscreen'].set_image(icon)


    def hide_application(self, data1=None, data2=None):
        self.gui.frontend['mainwin'].hide_all()
        self.gui.visible = False

        return True

    def do_nothing(self, *args):
        pass
        #print "%s" % args


    def run(self):
        id = self.gui.model['vm'].root



    def show_application(self, widget=None, ev=None, data=None):
        if self.gui.property['visible']:
            self.hide_application()
            self.gui.property['visible'] = False
        else:
            self.gui.frontend['mainwin'].maximize()
            self.gui.frontend['mainwin'].show_all()
            self.gui.property['visible'] = True
            self.gui.manager.show_start_here()
            self.activate_filter(None)
            self.gui.editor.hide_dialog()

        return True


    def show_resource(self, view, path, column):
        id = view.getSelectedItem(0)
        self.display_resource(id)
        #cursor-changed: print view.getSelectedItem(0)

    def stop(self, nothing=None):
        self.stop_frontend()
        self.log.info("Frontend stopped")
        self.gui.app.stop_backend()


    def stop_all_timeouts(self):
        self.gui.timeouts.stop_all()


    def stop_frontend(self):
        #self.stop_all_timeouts()
        gtk.main_quit()

    def tag_resource(self, *args):
        pass #print args

    def annotate_resource(self, *args):
        pass #print args

    def unfullscreen(self):
        self.gui.frontend['menu'].show_all()
        icon = gtk.image_new_from_stock(gtk.STOCK_FULLSCREEN, gtk.ICON_SIZE_BUTTON)
        self.gui.frontend['mainwin'].unfullscreen()
        self.gui.frontend['fullscreen'].set_image(icon)

    def show_all_resources(self, *args):
        """ Function doc """
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT distinct ?id
                WHERE
                {
                    ?id rdf:type ?object .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """
        #self.gui.frontend['sh-scolclipstats'].show_all()
        #self.gui.frontend['sh-sresources'].show_all()

        result = self.gui.ask.this(query)
        self.gui.frontend['view']['selected'] = result.selected
        self.gui.frontend['view']['query'] = query
        self.update_gui()

    def show_only_collections(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT distinct ?id
                WHERE
                {
                    ?id rdf:type pimo:Collection .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """
        #self.gui.frontend['sh-scolclipstats'].show_all()
        #self.gui.frontend['sh-sclipboard'].hide_all()
        #self.gui.frontend['sh-scollections'].show_all()
        ##self.gui.frontend['sh-sresources'].hide_all()

        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)
        self.gui.frontend['view']['query'] = query
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_resources(self, widget, query=None):
        """
        PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
        PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
        PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
        PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
        PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

        SELECT distinct ?id
        WHERE
        {
            ?id rdf:type ?object .
            ?id nao:created ?date .
            FILTER (rdf:type != pimo:Collection)
        }
        ORDER BY DESC(?date)
        """
        filter = self.gui.actions.get_date_filter()
        query = get_date_filtered_query(int(filter))
        #self.gui.frontend['sh-sresources'].show_all()

        result = self.gui.ask.this(query)
        self.gui.frontend['view']['selected'] = result.selected
        self.gui.frontend['view']['query'] = query
        self.update_gui()

    def show_only_clipboard(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Clipboard .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """
        #self.gui.frontend['sh-scolclipstats'].show_all()
        #self.gui.frontend['sh-scollections'].hide_all()
        ##self.gui.frontend['sh-sresources'].hide_all()

        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)
        self.gui.frontend['view']['query'] = query
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_audio(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Audio .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """

        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)

        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_text(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:TextDocument .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """
        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)

        #result = self.gui.ask.this(query)
        self.gui.frontend['view']['selected'] = result.selected
        self.gui.frontend['view']['query'] = query

        #self.gui.frontend['sh-sresources'].show_all()
        self.update_gui()
        #self.gui.frontend['sh-ntboper'].hide_all()

    def show_only_image(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Image .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """

        #self.gui.frontend['sh-sresources'].show_all()
        self.gui.frontend['view']['query'] = query
        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_video(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Video .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """

        #self.gui.frontend['sh-sresources'].show_all()
        self.gui.frontend['view']['query'] = query
        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_web(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Website .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """
        #self.gui.frontend['sh-sresources'].show_all()
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query)
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_feed(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Feed .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """
        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_remote(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:RemoteDataObject .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """

        #self.gui.frontend['sh-sresources'].show_all()
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query)
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def show_only_application(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Application .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """


        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)

        self.gui.frontend['view']['selected'] = result.selected
        self.gui.frontend['view']['query'] = query
        self.update_gui()

    def show_only_folder(self, *args):
        query = """
                PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
                PREFIX nie: <http://www.semanticdesktop.org/ontologies/nie/>
                PREFIX nfo: <http://www.semanticdesktop.org/ontologies/nfo/>
                PREFIX nao: <http://www.semanticdesktop.org/ontologies/nao/>
                PREFIX pimo: <http://www.semanticdesktop.org/ontologies/pimo/>

                SELECT ?id
                WHERE
                {
                    ?id rdf:type nfo:Folder .
                    ?id nao:created ?date .
                }
                ORDER BY DESC(?date)
                """

        #self.gui.frontend['sh-sresources'].show_all()
        self.gui.frontend['view']['query'] = query
        mygraph = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['current'])
        self.gui.frontend['view']['query'] = query
        result = self.gui.ask.this(query, mygraph)
        self.gui.frontend['view']['selected'] = result.selected
        self.update_gui()

    def update_gui(self, filterdate=True):
        store = None
        self.gui.frontend['view']['previous'] = self.gui.frontend['view']['current']
        self.gui.frontend['view']['current'] = self.gui.frontend['view']['selected']
        #print "previous", self.gui.frontend['view']['previous']
        #print "current", self.gui.frontend['view']['current']
        #print "selected", self.gui.frontend['view']['selected']
        try:
            store = self.gui.ask.create_temporal_graph(self.gui.frontend['view']['selected'])
            #self.gui.manager.update_stats(store)
            cloud = self.gui.ask.make_report(store)
            qnames = []
            bnames = []
            qnames.append('-')
            bnames.append('?')
            for ns in cloud:
                for qname in cloud[ns]:
                    key = ns + ':' + qname
                    qnames.append(key)
                    bnames.append(beautiful_name(qname))
            set_model_combobox_from_list(self.gui.frontend['sh-cmbqnames'], qnames, bnames)
            self.gui.frontend['cloud'] = cloud
        except Exception, error:
            pass

        self.gui.manager.starthere.update_resources_list(filterdate)
        self.gui.manager.starthere.update_stats(store)
        self.gui.last_query = datetime.datetime.now()
        self.gui.colview.update_collection_view()
        #self.gui.clpview.update_clipboard_view()
        """
        if self.gui.manager.num_pages() <= 1:
            self.gui.frontend['notebook'].set_show_tabs(False)
        else:
            self.gui.frontend['notebook'].set_show_tabs(True)
        """

        return True


    def update_statusbar(self, message):
        self.gui.statusbar.set_text(message)


    def show_about_dialog(self, *args):
        dialog_about = AboutDialog(self.gui)
        dialog_about.run()

    def show_preferences(self):
        self.gui.manager.show_preferences()


    def show_resources_listview(self, *args):
        self.gui.frontend['preferences']['resources_view'] = 'listview'
        self.update_gui()
        """
        query = self.gui.frontend['view']['query']
        self.show_only_resources(None, query)
        """

    def show_resources_iconview(self, *args):
        self.gui.frontend['preferences']['resources_view'] = 'iconview'
        self.update_gui()

    def show_collections_listview(self, *args):
        self.gui.frontend['preferences']['collections_view'] = 'listview'
        self.update_gui()

    def show_collections_iconview(self, *args):
        self.gui.frontend['preferences']['collections_view'] = 'iconview'
        self.update_gui()

    def show_clipboard_listview(self, *args):
        self.gui.frontend['preferences']['clipboard_view'] = 'listview'
        self.update_gui()

    def show_clipboard_iconview(self, *args):
        self.gui.frontend['preferences']['clipboard_view'] = 'iconview'
        self.update_gui()

    def activate_date_filter(self, combobox, *data):
        t1 = datetime.datetime.now()
        model = self.gui.frontend['sh-cmbfdate'].get_model()
        iter = combobox.get_active_iter()
        filter = model.get_value(iter, 0)

        if filter == '-':
            # avoid date filtering
            return

        query = get_date_filtered_query(int(filter))

        result = self.gui.ask.this(query)
        self.gui.frontend['view']['selected'] = result.selected
        self.gui.frontend['view']['query'] = query
        t2 = datetime.datetime.now()
        tf1 = t2 - t1
        t1 = datetime.datetime.now()
        self.update_gui()
        t2 = datetime.datetime.now()
        tf2 = t2 - t1
        text = "<b>Filter</b>: <i>%s secs</i>\t\t<b>GUI</b>: <i>%s secs</i>" % (tf1, tf2)
        self.gui.statusbar.set_text(text)


    def activate_filter(self, combobox, *data):
        try:
            model = combobox.get_model()
            iter = combobox.get_active_iter()
            filter = model.get_value(iter, 0)
            ns, qname = filter.split(':')
            data = self.gui.frontend['cloud'][ns][qname]

            old = self.gui.frontend['sh-cntcloud'].get_children()
            if old:
                self.gui.frontend['sh-cntcloud'].remove(old[0])
                del old[0]

            key =  ns + ':' + qname
            wdgcloud = Cloud(key, data, self.activate_filter_property)
            container = gtk.ScrolledWindow()
            container.set_shadow_type(gtk.SHADOW_NONE)
            container.add_with_viewport(wdgcloud)
            container.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
            parent = wdgcloud.get_parent()
            parent.set_shadow_type(gtk.SHADOW_NONE)
            wc, hc = wdgcloud.size_request()
            ww, hw = self.gui.frontend['mainwin'].size_request()
            if wc > ww:
                wc = ww
            if hc < hw:
                container.set_size_request(wc, hc + 50)
            else:
                container.set_size_request(wc, 200)

            self.gui.frontend['sh-cntcloud'].add(container)

            self.gui.frontend['sh-cntcloud'].show_all()
            self.gui.frontend['sh-frmfilter'].show_all()
        except Exception, error:
            self.gui.frontend['sh-frmfilter'].hide_all()

    def activate_filter_property(self, button, value):
        try:
            t1 = datetime.datetime.now()
            predicate = button.get_data("property")
            ns, qname = predicate.split(':')

            if ns == 'rdf':
                parts = value.split('/')
                l = len(parts)
                object = parts[l - 2] + ':' + parts[l - 1]
            else:
                object = Literal(value)

            #self.update_statusbar("using filter <b>%s</b> with value '%s'..." % (predicate, object))

            #filter = self.gui.actions.get_date_filter()
            filtered_query = get_filtered_query(predicate, object)
            result = self.gui.ask.this(filtered_query)
            self.gui.frontend['view']['selected'] = result.selected
            self.gui.frontend['view']['query'] = filtered_query
            t2 = datetime.datetime.now()
            tf1 = t2 - t1

            t1 = datetime.datetime.now()
            self.update_gui(filterdate=False)
            t2 = datetime.datetime.now()
            tf2 = t2 - t1
            text = "Filter <b>%s</b> with value <b>%s</b>: <i>%s secs</i>\t\t<b>GUI</b>: <i>%s secs</i>" % (predicate, value, tf1, tf2)
            self.gui.statusbar.set_text(text)
            #self.update_statusbar("using filter '<b>%s</b>' with value '<b>%s</b>' took <b>%s secs</b>" % (property, value, (t2 - t1)))
            #self.log.debug("activating property '%s' with value '%s' in %s secs" % (property, value, (t2 - t1)))
        except Exception, error:
            self.log.error(error)

    def show_edit_resource_dialog(self, *args):
        pass
        #self.gui.frontend['sh-ntboper'].set_current_page(0)
        #self.gui.frontend['sh-ntboper'].show_all()

    def show_previous_view(self, *args):
        self.gui.frontend['view']['selected'] = self.gui.frontend['view']['previous']
        self.update_gui()


    def show_clipboard_view(self, *data):
        self.gui.clpview.toggle_view()

    def show_collection_view(self, *data):
        self.gui.colview.toggle_view()
        #totres = self.gui.ask.get_total_resources(None)
        #cache = len(self.gui.app.vstore.cache)
        #text = "<b>Cache</b>: %d\t<b>Store</b>: %d" % (cache, totres)
        #self.gui.statusbar.set_text(text)



    def add_new_resource(self, widget=None, operation=None):
        self.gui.addresdlg.show_window(operation)

    def add_new_collection(self, widget, *data):
        self.gui.addresdlg.show_window('collection')

    def edit_selected_collection(self, widget, treeview):
        self.log.debug("Delete selected collection: not implemented yet")

    def setup_date_filter(self):
        (NOFILTER, LASTMIN, LAST1H, LAST6H, LAST12H, LASTDAY, WEEK, MONTH, SIXMONTHS, YEAR, ALWAYS) = range(11)
        qnames = []
        bnames = []
        qnames.append('-')
        bnames.append('?')
        qnames.append(LASTMIN)
        bnames.append(_("last 30 minutes"))
        qnames.append(LAST1H)
        bnames.append(_("last hour"))
        qnames.append(LAST6H)
        bnames.append(_("last 6 hours"))
        qnames.append(LAST12H)
        bnames.append(_("last 12 hours"))
        qnames.append(LASTDAY)
        bnames.append(_("last day"))
        qnames.append(WEEK)
        bnames.append(_("last week"))
        qnames.append(MONTH)
        bnames.append(_("last month"))
        qnames.append(SIXMONTHS)
        bnames.append(_("last six months"))
        qnames.append(YEAR)
        bnames.append(_("last year"))
        qnames.append(ALWAYS)
        bnames.append(_("always"))

        set_model_combobox_from_list(self.gui.frontend['sh-cmbfdate'], qnames, bnames) #, False)
        self.gui.frontend['sh-cmbfdate'].set_active(1)



